package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvName, tvGender, tvPhone, tvHighScore;
    private Button btnEditProfile, btnStartGame;
    
    // 玩家信息
    private String playerName = "艾雄";
    private String playerGender = "男";
    private String playerPhone = "13800138000";
    private int highScore = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initViews();
        setupClickListeners();
        updatePlayerInfo();
    }

    private void initViews() {
        tvName = findViewById(R.id.tvName);
        tvGender = findViewById(R.id.tvGender);
        tvPhone = findViewById(R.id.tvPhone);
        tvHighScore = findViewById(R.id.tvHighScore);
        btnEditProfile = findViewById(R.id.btnEditProfile);
        btnStartGame = findViewById(R.id.btnStartGame);
    }

    private void setupClickListeners() {
        btnEditProfile.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, EditProfileActivity.class);
            intent.putExtra("name", playerName);
            intent.putExtra("gender", playerGender);
            intent.putExtra("phone", playerPhone);
            startActivityForResult(intent, 1);
        });

        btnStartGame.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, CardGameActivity.class);
            startActivityForResult(intent, 2);
        });
    }

    private void updatePlayerInfo() {
        tvName.setText(playerName);
        tvGender.setText(playerGender);
        tvPhone.setText(playerPhone);
        tvHighScore.setText(String.valueOf(highScore));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                // 从编辑页面返回
                playerName = data.getStringExtra("name");
                playerGender = data.getStringExtra("gender");
                playerPhone = data.getStringExtra("phone");
                updatePlayerInfo();
            } else if (requestCode == 2) {
                // 从游戏页面返回，更新最高分
                int newScore = data.getIntExtra("score", 0);
                if (newScore > highScore) {
                    highScore = newScore;
                    updatePlayerInfo();
                }
            }
        }
    }
}